package activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import bean.personal_bean.PayMoneyBean;
import bean.personal_bean.ZhiFuBaoBandingTypebean;
import presenter.SysTemPresenter;
import util.utls.ByAlert;
import util.utls.EditTextWithDel;
import util.utls.IsUtilUid;
import util.utls.UIByaToast;
import util.volley.ResultEntity;

/**
 * Created by itboye on 2016/12/12.
 */

public class ActivityChongZhi extends BaseActivity implements Observer {
    List<ZhiFuBaoBandingTypebean> typebean;

    RelativeLayout relyoutZhiFuBao, relyoutWeiXin;
    EditTextWithDel chongzhi_money;
    PayMoneyBean bean;
    SysTemPresenter presenter;
    String AccId;
    TextView chongzhi_ok;
    UIByaToast uiAlertView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chong_zhi);
        typebean = new ArrayList<ZhiFuBaoBandingTypebean>();
        bean = new PayMoneyBean();
        presenter = new SysTemPresenter(this);
        presenter.onBangDinType();
        showProgressDialog("数据加载中...",true);
    }

    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.chongzhi_ok:

                if (chongzhi_money.getText().toString().trim() == null || chongzhi_money.getText().toString().trim() == "") {
                    ByAlert.alert("请输入充值金额");
                } else {
                    showProgressDialog("充值中...",true);
                    presenter.onChongZhi(IsUtilUid.isUid(), chongzhi_money.getText().toString());

                }
                break;
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            IsUtilUid.tianjia(entity);
            if (entity.getEventType() == presenter.BangdingType_success) {
                typebean = (List<ZhiFuBaoBandingTypebean>) entity.getData();
                AccId = typebean.get(0).getId();
                Log.d("AcctiId", AccId);
                if (AccId.equals("6179")) {
                    relyoutZhiFuBao.setVisibility(View.VISIBLE);
                    relyoutWeiXin.setVisibility(View.GONE);
                }
                try{closeProgressDialog();}catch (Exception ex){}

            }
            if (entity.getEventType() == presenter.ChongZhi_success) {
                bean = (PayMoneyBean) entity.getData();
                if (bean.getPay_money().equals("0")) {
                    ByAlert.alert("充值失败");
                } else {

                    uiAlertView = new UIByaToast(ActivityChongZhi.this, "", "充值成功!请到钱包查看!", "", "确定");
                    uiAlertView.show();

                    uiAlertView.setClicklistener(new UIByaToast.ClickListenerInterface() {

                        public void doLeft() {
                            uiAlertView.dismiss();
                        }

                        public void doRight() {
                            uiAlertView.dismiss();
                            Intent intent = new Intent("uiAlertView");
                            sendBroadcast(intent);
                            finish();

                        }
                    });
                    try{closeProgressDialog();}catch (Exception ex){}

                }

            }

        }
    }


}
